<?php
namespace app\data_magic\controller;

use cmf\controller\AdminBaseController;
use app\data_magic\logic\Interpreter;
use app\data_magic\logic\DataList;


class DemoCategoryController extends AdminBaseController
{
    public function showList()
    {
        $interpreter=new Interpreter();
        $meta=$interpreter->loadScene();
        $logic=new DataList();
        $list=$logic->showList($meta);
        $widget=new WidgetController();
        return $widget->fetchTable($meta,$list);
    }
    public function showCreateForm()
    {
        $interpreter=new Interpreter();
        $meta=$interpreter->loadScene();
        $logic=new DataList();
        $item=$logic->showCreateItem($meta);
        $widget=new WidgetController();
        return $widget->fetchForm($meta,$item);
    }
    public function insert()
    {
        $interpreter=new Interpreter();
        $meta=$interpreter->loadScene();
        $widget=new WidgetController();
        $item=$widget->convertInputData($meta);
        $logic=new DataList();
        $result=$logic->createItem($meta,$item);
        if(!$result){
            $this->error($logic->getError());
        }
        $this->success();
    }
    public function showEditForm()
    {
        $interpreter=new Interpreter();
        $meta=$interpreter->loadScene();
        $logic=new DataList();
        $item=$logic->showItem($meta,input("data_id"));
        $widget=new WidgetController();
        return $widget->fetchForm($meta,$item);
    }
    public function update()
    {
        $interpreter=new Interpreter();
        $meta=$interpreter->loadScene();
        $widget=new WidgetController();
        $item=$widget->convertInputData($meta);
        $logic=new DataList();
        $result=$logic->updateItem($meta,input("data_id"),$item);
        if(!$result){
            $this->error($logic->getError());
        }
        $this->success();
    }
    public function delete()
    {
        $interpreter=new Interpreter();
        $meta=$interpreter->loadScene();
        $logic=new DataList();
        $result=$logic->deleteItem($meta,input("data_id"));
        if(!$result){
            $this->error($logic->getError());
        }
        $this->success();
    }
}
